package app.controller;

import app.auth.AccessManager;
import app.common.Result;
import app.dao.MongoDBConnection;
import app.utils.Views;
import app.vo.SearchVo;
import app.vo.User;
import io.javalin.http.Context;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class HomeController {
    private static List<User> USERS = new ArrayList<>();

    public static void toHome(Context ctx) {
        Map<String, Object> model = Views.baseModel(ctx);
        ctx.render("/views/home.html", model);
    }

    public static void login(Context ctx) {
        getUsers();
        String username = ctx.formParam("username");
        String password = ctx.formParam("password");
        boolean flag = false;
        for (User e:USERS) {
            if (e.getUsername().equals(username) && e.getPassword().equals(password)){
                AccessManager.loginUser(ctx, e);
                flag = true;
            }
        }
        if (flag) {
            ctx.result(Result.ok("Login Succeeded"));
        } else {
            ctx.result(Result.error("Username Or Password Incorrect"));
        }
    }

    public static void getHost(Context ctx) {
        SearchVo vo = new SearchVo();
        Map<String, Object> map = new HashMap<>();
        vo.setLocation(ctx.formParam("location"));
        vo.setProperty(ctx.formParam("property"));
        vo.setAmenities(ctx.formParam("amenities"));
        vo.setSummary(ctx.formParam("summary"));
        vo.setStatus(ctx.formParam("status"));
        vo.setBedrooms(Integer.parseInt(Objects.requireNonNull(ctx.formParam("bedrooms"))));
        vo.setBeds(Integer.parseInt(Objects.requireNonNull(ctx.formParam("beds"))));
        vo.setAccommodated(Integer.parseInt(Objects.requireNonNull(ctx.formParam("accommodated"))));
        vo.setRating(Integer.parseInt(Objects.requireNonNull(ctx.formParam("rating"))));
        vo.setPrice(Double.parseDouble(Objects.requireNonNull(ctx.formParam("price"))));
        int pageNum = Integer.parseInt(Objects.requireNonNull(ctx.formParam("pageNum")));
        long total = MongoDBConnection.getConnection().pageCount(vo);
        if (total > 0) {
            map.put("data", MongoDBConnection.getConnection().page(vo, pageNum));
            map.put("total", total);
        } else {
            map.put("data", new ArrayList<>());
            map.put("total", 0);
        }
        ctx.result(Result.ok(map));
    }

    public static void getUsers() {
        USERS.add(new User("1","admin", "admin"));
        USERS.add(new User("2", "user1", "user1"));
        USERS.add(new User("3", "user2", "user2"));
    }

    public static void toHost(Context ctx) {
        User user = AccessManager.getSessionCurrentUser(ctx);
        if (null == user) {
            ctx.redirect("/");
        }
        String id = ctx.pathParam("id");
        Map<String, Object> model = Views.baseModel(ctx);
        model.put("id", id);
        ctx.render("/views/info.html", model);
    }

    public static void hostInfo(Context ctx) {
        User user = AccessManager.getSessionCurrentUser(ctx);
        if (null == user) {
            ctx.redirect("/");
        }
        String id = ctx.pathParam("id");
        int pageNum = Integer.parseInt(Objects.requireNonNull(ctx.queryParam("pageNum")));
        ArrayList<Object> host = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNoneEmpty(id)){
            host = MongoDBConnection.getConnection().getHost(id);
        }
        if (host.size() > 0){
            map.put("host", host.get(0));
            long total = MongoDBConnection.getConnection().hostComCount(id);
            if (total > 0) {
                map.put("com", MongoDBConnection.getConnection().getHostCom(id, pageNum));
                map.put("total", total);
            }
            ctx.result(Result.ok(map));
        } else {
            ctx.result(Result.error("Information does not exist"));
        }
    }

    public static void addCom(Context ctx) {
        User user = AccessManager.getSessionCurrentUser(ctx);
        if (null == user) {
            ctx.redirect("/");
        }
        String hostId = ctx.formParam("id");
        String content = ctx.formParam("content");
        String name = ctx.formParam("name");
        MongoDBConnection.getConnection().insertCom(user, hostId, content, name);
        ctx.result(Result.ok());
    }
    public static void updateCom(Context ctx) {
        User user = AccessManager.getSessionCurrentUser(ctx);
        if (null == user) {
            ctx.redirect("/");
        }
        String id = ctx.formParam("id");
        String content = ctx.formParam("content");
        if (MongoDBConnection.getConnection().updateCom(id, content)) {
            ctx.result(Result.ok());
        } else {
            ctx.result(Result.error());
        }

    }
    public static void deleteCom(Context ctx) {
        User user = AccessManager.getSessionCurrentUser(ctx);
        if (null == user) {
            ctx.redirect("/");
        }
        String id = ctx.formParam("id");
        if (MongoDBConnection.getConnection().deleteCom(id)) {
            ctx.result(Result.ok());
        } else {
            ctx.result(Result.error());
        }
    }

    public static void toCenter(Context ctx) {
        User user = AccessManager.getSessionCurrentUser(ctx);
        if (null == user) {
            ctx.redirect("/");
        }
        Map<String, Object> model = Views.baseModel(ctx);
        ctx.render("/views/center.html", model);
    }
    public static void getMyCom(Context ctx) {
        User user = AccessManager.getSessionCurrentUser(ctx);
        if (null == user) {
            ctx.redirect("/");
        }
        Map<String, Object> map = new HashMap<>();
        int pageNum = Integer.parseInt(Objects.requireNonNull(ctx.formParam("pageNum")));
        long total = MongoDBConnection.getConnection().hostComCountByUserId(user.getId());
        if (total > 0) {
            map.put("com", MongoDBConnection.getConnection().getHostComByUserId(user.getId(), pageNum));
            map.put("total", total);
        } else {
            map.put("com", new ArrayList<>());
            map.put("total", total);
        }
        ctx.result(Result.ok(map));
    }
    public static void logout(Context context) {
        AccessManager.logout(context);
        context.redirect("/");
    }
}
